package production.practice.utils;

import java.lang.reflect.Field;
import java.time.LocalDateTime;

/**
 * 验证正则表达式的
 */
public class InfoKit {
    /**
     * 验证手机号格式（国内手机号规则：11位，以13/14/15/17/18/19开头）
     */
    public static boolean isValidPhone(String phone) {
        if (phone == null || phone.length() != 11) {
            return false;
        }
        return phone.matches("^1[3-9]\\d{9}$");
    }

    /**
     * 验证邮箱格式
     */
    public static boolean isValidEmail(String email) {
        if (email == null || email.isEmpty()) {
            return false;
        }
        // 邮箱正则表达式（支持常见格式）
        String regex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
        return email.matches(regex);
    }

    /**
     * 验证所有字段非空,
     * 同时反射获取了字段后,为时间反射赋值
     */

    public static boolean isValidAll(Object obj) throws NoSuchFieldException, IllegalAccessException {

        if (obj == null) return false;
        //反射获取createTime字段,并且赋值

        Class<?> aClass = obj.getClass();
        Field createTimeField = aClass.getDeclaredField("createTime");
        createTimeField.setAccessible(true);
        createTimeField.set(obj, LocalDateTime.now());


        //检验是否合格有效
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
                Object value = field.get(obj);
                if (value == null && field.getName() != "id") {
                    return false;
                }
        }
        return true;
    }
}
